package wator.util;

import java.util.Date;
import java.util.Vector;
import java.io.*;

public class GnuplotWritter<T> {
	 
	private String filename;
	private PrintStream stream;
	FileOutputStream fout;
	boolean opened=false;
	
	
	public GnuplotWritter(){}
	
	public GnuplotWritter(String name) throws FileNotFoundException
	{
		open(name);
	}
	
	@SuppressWarnings("deprecation")
	public static String generateSimulationName()
	{
		Date date=new Date();
		return "sim_"+(date.getMonth()+1)+"-"+date.getDate()+"_"+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
	}
	
 	public void open(String fname) throws FileNotFoundException
	{
		fout = new FileOutputStream(fname);
		stream=new PrintStream(fout);
		opened=true;
	}
	
	public void close() throws IOException
	{
		if(opened)
			fout.close();
	}
	
	public void write(String string)
	{
		if(opened)
		{
			stream.print(string);
		}
		else
			System.out.println(	"GnuploWritter could not write in the file." +
								"\nPlease use open() first.");
	}
	
	public void write(Vector<T> vector)
	{
		if(opened)
		{
			for(int i=0;i<vector.size();i++)
				stream.print(vector.get(i)+"\t");
			stream.print("\n");
		}
		else
			System.out.println(	"GnuploWritter could not write in the file." +
								"\nPlease use open() first.");
	}
	
	public void write(T x,T y)
	{
		Vector<T> v=new Vector<T>();
		v.addElement(x);
		v.addElement(y);
		write(v);
	}
	
	public void write(T x,T y,T z)
	{
		Vector<T> v=new Vector<T>();
		v.addElement(x);
		v.addElement(y);
		v.addElement(z);
		write(v);
	}
	
	public void writeConfig(String key,String value)
	{
		stream=new PrintStream(fout);
		if(opened)
		{
			stream.println(key+"\t"+value);
		}
		else
			System.out.println(	"GnuploWritter could not write in the file." +
								"\nPlease use open() first.");
	}

	public String getFilename() {
		return filename;
	}
	
}
